// ==UserScript==
// @name           Boxcar - DreamCatalyst enhancements
// @namespace      http://doireallyneedadomainname.com
// @description    Fixes a small number of issues with usability.
// @include        http://www.boxcarhosting.net/Boxcar/portal/*
// ==/UserScript==


// ==================================================================== //
// Readability fixes  
// ==================================================================== //

var body = document.getElementsByTagName("body")[0];
body.style.backgroundColor = "#D6DFFF";   // Change this if you want/like

var fontElems = document.getElementsByTagName("font");
for(var i = fontElems.length -1; i >= 0; i--) {
  var e = fontElems[i];
  if(e.color.toLowerCase() == "yellow")
    e.style.backgroundColor = "#858585";
}


// ==================================================================== //
// Market selector fix - Selects the filter that's in use
// ==================================================================== //

var uri = "" + document.location;
if(uri.match("marketnews.php")) {
  var contentDiv = document.getElementById("content");
  var currentSelection = contentDiv.getElementsByTagName("span")[1].innerHTML;
  
  if(currentSelection != "") {
    var selectElem = contentDiv.getElementsByTagName("select")[0];
    var optionElems = selectElem.getElementsByTagName("option");
    for(var i = optionElems.length -1; i >= 0; i--) {
      if(optionElems[i].value == currentSelection) {
        optionElems[i].setAttribute("selected", "true");
        break;
      }
    }
  }
}


// ==================================================================== //
// Market ticker fix - Repositions the ticker to make it fully visible
// ==================================================================== //

var topleft = document.getElementById("topleft");
topleft.style.width = "20%";

var headercontent = document.getElementById("headercontent");
headercontent.style.width = "79%";

var header = document.getElementById("header");
header.style.height = "130px";
header.style.width = "100%";



// ==================================================================== //
// Defending/Attacking news - Adds a button to filter out self-hits
// ==================================================================== //

function toggleSelfhitsVisibility(show) {
  var contentDiv = document.getElementById("content");
  var tableElem = contentDiv.getElementsByTagName("table")[0];
  var trElems = tableElem.getElementsByTagName("tr");
  
  for(var i=0; i<trElems.length; ++i) {
    var list = trElems[i].getElementsByTagName("font");
    if(list.length >= 4) {
      var clan1 = list[1].innerHTML;
      var clan2 = list[3].innerHTML;
      if(clan1 == clan2) {
	trElems[i].style.visibility = show? "visible" : "collapse";
      }
    }
  }
}

function filterChanged(evt) {
  toggleSelfhitsVisibility(evt.target.checked);
}

var uri = "" + document.location;
if(uri.match("defending.php") || uri.match("attacking.php")) {
  var contentDiv = document.getElementById("content");
  var tableElem = contentDiv.getElementsByTagName("table")[0];
  
  var filterButton = document.createElement("input");
  filterButton.type = "checkbox";
  filterButton.checked = true;
  contentDiv.insertBefore(filterButton, tableElem);
  contentDiv.insertBefore(document.createTextNode("Show selfhits"), tableElem);
  filterButton.addEventListener("click", filterChanged, true);
}

